<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}
/**
 * OrderClass 訂單操作類
 * add()
 * del()
 * makesn()
 * edit_money_state() 修改付款狀態1已付款 0未附款
 * edit_shipping_state() 修改發貨狀態同上
 * getdata()
 *
 */

class OrderClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//設置全局變量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."order";
	}
	function GroupBondClass() {
		$this->__construct();
	}
	/**
	 * 添加訂單入庫
	 *
	 * @param int $gid
	 * @param int $num
	 * @param int $totalprice
	 * @param int $uid
	 * @param string $uname
	 * @return OK/提示
	 */
	function add($garray){
		$timestamp=time();
		if(is_numeric($garray['gid']) && is_numeric($garray['num']) && is_numeric($garray['order_total_price']) && is_numeric($garray['uid'])){
			$sn=$this->makesn($garray['gid'],$garray['uid']);//隨機生成訂單編號
			$garray['order_sn']=$sn;//設置訂單字段
			
			$row = $this->db->fetch_first("Select sid,gtypeid,shop_price,title From {$this->tablepre}groups where gid = '{$garray['gid']}'");
			if(is_array($row)){
				$garray['gtitle']=$row['title'];
				$garray['gtypeid']=$row['gtypeid'];
				$garray['group_fee']=$row['shop_price'];
				$garray['order_addtime']=$timestamp;
				$garray['order_updatetime']=$timestamp;
				
				$oid=inserttable($this->table, $garray,1);
				//插入數據庫操作	
				if(is_numeric($oid))return $oid;
				//$this->db->query("insert into {$this->table}(order_sn,gid,gtitle,num,order_addtime,order_total_price,uid,uname) VALUES('{$sn}','{$gid}','{$row[title]}',$num,$timestamp,$totalprice,$uid,'$uname')");						
				
			}else return "團購ID不存在";
		}else return "缺少參數";
	}
	/**
	 * 編輯訂單
	 *
	 * @param unknown_type $oid
	 * @param unknown_type $garray
	 * @return OK/提示
	 */
	function edit($oid,$garray){
		$timestamp=time();
		if (GetNum($oid) && is_array($garray)) {
			//更新數據庫操作
			$wheresqlarr=" oid='{$oid}'";
			updatetable($this->table,$garray, $wheresqlarr);
			return "OK";
		}else return "缺少參數";
		
	}
	
	
	
	/**
	 * 隨機生成訂單編號
	 *
	 * @param int $gid
	 * @param int $uid
	 * @return string
	 */
	function makesn($gid=0,$uid=0){
		$autokeys=randomkeys(5,"123");//隨機5位數字
		$timestr=date('Ymd');
		$sn=$gid."_".$uid.$timestr.$autokeys;
		return $sn;
	}
	/**
	 * 修改付款狀態 
	 * 收款狀態	0沒收款	1部分收款	2全部收款	3退款
	 * @param int $oid
	 * @param int $num
	 * @return true/false
	 */
	function edit_money_state($oid,$num=0,$money=0 , $paytype){
		$value=$this->getone($oid);
		if(empty($value)){
			//會員充值反饋處理
			include_once(INC_PATH."/accountcharge.class.php");
			$ac=new AccountChargeClass();
			$info=$ac->edit_money_state($oid,$money);
			return $info;
		}
		$oid=$value['oid'];
		if ($value['money_state']==2) {
			$groupcount=addfield("users","groupcount","uname='{$value['uname']}'",0,true);//修改用戶購買團購次數 返回現在數
			if ($groupcount==1) {
				include_once(INC_PATH."/referrals.class.php"); //包含團購類
				$ref=new Referralsclass();
				$ref->add($value['gid'],$value['gtitle']);//檢查是否有人邀請有的話產生邀請記錄
			}
			return true;
		}
		
		if(($num==0 || $num==1 || $num==2)&&is_numeric($oid)){
			if ($money!=0) {
				if ($money+$value['order_incharge'] >= $value['order_total_price']) {
					$num=2;
				}else $num=1;
			}
			$this->db->query("update `$this->table` SET money_state = {$num},order_incharge = order_incharge+$money WHERE oid='{$oid}'");
			include_once(INC_PATH."/group.class.php"); //包含團購類
			$gp=new GroupClass();
			$gp->buynum($value['gid'],$value['num']);
			if ($num==2) {
				//綁定團購卷給用戶
				if ($value['gtypeid']==0) {
					include_once(INC_PATH."/groupbond.class.php"); //包含團購類
					$bond=new GroupBondClass();
					$bond->binduser($value['uid'],$value['uname'],$value['order_sn'],$value['gid'],$value['num']);//綁定給用戶
				}
				
				$groupcount=addfield("users","groupcount","uname='{$value['uname']}'",1,true);//修改用戶購買團購次數 返回現在數
				if ($groupcount==1) {
					include_once(INC_PATH."/referrals.class.php"); //包含團購類
					$ref=new Referralsclass();
					$ref->add($value['gid'],$value['gtitle']);//檢查是否有人邀請有的話產生邀請記錄
				}
				//記錄交易金額
				include_once(INC_PATH."/moneychanges.class.php");
				$mc=new MchangesClass();
				$paytype = !empty($paytype) ? $paytype : "線上付款"; // 付款方式
				
				$mc->add($value['order_sn'],$value['order_total_price'],$money,$paytype);
				
				
			}
			return true;
		}else return false;	
		
	}
	
	/**
	 * 修改發貨狀態
	 *參數 0未發貨 1 已發貨
	 * @param int $oid
	 * @param int $num
	 * @return true/false
	 */
	function edit_shipping_state($oid,$num=0){
		if(($num==0 || $num==1)&&is_numeric($oid)){
			$this->db->query("update `$this->table` SET shipping_state = {$num} WHERE oid={$oid}");
			return true;
		}else return false;	
	}
	/**
	 * 刪除指定ID訂單
	 *
	 * @param int $oid
	 * @return true/false
	 */
	function del($oid){
		if(is_numeric($oid)){
			$this->db->query("delete from {$this->table} where oid={$oid}");
			return true;
		}return false;	
	}
	
	/**
	 * 獲取一條信息
	 *
	 * @param unknown_type $oid
	 * @return unknown
	 */
	function getone($oid){
		if (!is_numeric($oid)) {
			$oid=$this->db->result_first("Select oid From `$this->table` WHERE order_sn='{$oid}'");
		}
		if (GetNum($oid)) {
			$gpdata=$this->getdata(1,"oid='{$oid}'");
			$value=$gpdata[0];
		}
		return $value;
		
	}
	
	/**
	 * 獲取數據數組
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby="",$type="",$field="*",$groupby="")
	{
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by oid desc";		
		if(!empty($groupby))$groupby=" group by $groupby ";
		if($type=="ALL")$addsql=" AS O left join {$this->tablepre}groups AS G ON O.gid=G.gid ";
		
		$sql="select {$field} from {$this->table}{$addsql}{$where}{$groupby}{$orderby}{$limit}";
		$query =$this->db->query($sql);
	
		$checkSubTitle=0;
		while($value = $this->db->fetch_array($query)) {
			//此處可進行數據整理						
			$tempdata[]=$value;
			
			//新增商品種類的名稱
			if(!$checkSubTitle){
			$detail = split(";",$value['orderdetail']);
			for($i=0;$i<sizeof($detail)-1;$i++)
			{
				$count = split("-",$detail[$i]);
				$title_id = $count[0];				
				$subtitle = "SELECT subtitle FROM {$this->tablepre}groups WHERE gid=".$count[0];				
				$result = $this->db->query($subtitle);
				$res = $this->db->fetch_array($result);				
				//$tempdata['subtitle'][] = $res['subtitle'];
			}
			$checkSubTitle++;
			}
	

		}
		return $tempdata;
	}
	function getcount($where=""){
		if(!empty($where))$where=" where $where";
		$count= $this->db->result_first("Select count(*) From `$this->table` $where");
		return $count;
	
	}
	function getsum($uname,$gid=""){
		if (!empty($uname)) {
			if($gid!="")$wheregid=" and gid={$gid}";else $wheregid="";
			$count= $this->db->result_first("Select SUM(num) From `$this->table` where uname='{$uname}' $wheregid and state<>2 and money_state=2 ");
			return $count;
		}else return 0;
	}
	
}
?>